﻿CREATE FUNCTION QIC.CallFactory(@ConfigName VARCHAR(50), @Token QIC.TokenTable READONLY, @CallerId INT, @CalleeId INT, @ReturnPropertName VARCHAR(50), @TokenMap VARCHAR(50), @Indent INT, @CalleeDelegateName varchar(50), @CallFactory VARCHAR(50))
RETURNS VARCHAR(MAX)
AS
BEGIN

	DECLARE @DelegateName VARCHAR(50)
	DECLARE @CallType VARCHAR(50)

	SELECT @CallType = type FROM sys.objects WHERE object_id = @CalleeId
	SET @DelegateName = @CallType

	IF @CallType <> @CallFactory AND EXISTS (SELECT * FROM QIC.Delegate_(@ConfigName) WHERE InterfaceSchemaName = 'QIC' AND InterfaceName = 'CallFactory_' AND DelegateName = @CallFactory)
	BEGIN
		SET @DelegateName = @CallFactory
	END
	
	RETURN QIC.CallFactory_(@DelegateName, @Token, @CallerId, @CalleeId, @ReturnPropertName, @TokenMap, @Indent, @CalleeDelegateName)

END

